package {
    import flash.filters.DisplacementMapFilter;
    import flash.filters.DisplacementMapFilterMode;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.Sprite;
    import flash.geom.ColorTransform;
    import flash.filters.BlurFilter;
    import flash.display.BlendMode;
    
    [SWF(frameRate="60", backgroundColor="0xffffff")]
    public class FlashTest extends Sprite {
        
        public var emitter:Emitter;
        
        public var canvas:BitmapData;
        public var buffer:BitmapData;
        
        private var iParticles:uint = 0;
        private var particlesLength:uint = 0;
        
        private var blurFilter:BlurFilter;
        private var blurFilter2:BlurFilter;
        private var ct:ColorTransform = new ColorTransform(1, 1, 1, .9, 255, 255, 255);
        private var _rect:Rectangle;
        private var _ep:Point = new Point;
        private var _mx:int, _my:int;
        public function FlashTest() {
			addChild(new Image.image);
            emitter = new Emitter(0,0,0,0,0,0,.9,.9,1,2,30,80,60,-0.3);
            
            canvas = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
            buffer = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
            
            _rect = canvas.rect;
            
            var map:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
                map.perlinNoise(84, 84, 5, Math.random()*100, false, true, 1, true);
            var bmp:Bitmap = new Bitmap(buffer);
                bmp.filters = [new DisplacementMapFilter(map, _ep, 1, 1, 64, 64, DisplacementMapFilterMode.CLAMP)];
            addChild(bmp);
            
            blurFilter = new BlurFilter(2, 2, 1);
            blurFilter2 = new BlurFilter(4, 4, 1);
            
            _mx = stage.stageWidth/2;
            _my = stage.stageHeight/2;
            
           
            stage.addEventListener(MouseEvent.MOUSE_MOVE, function(e:MouseEvent):void
            {              
                _mx = stage.mouseX;
                _my = stage.mouseY;
            });
          
 
            addEventListener(Event.ENTER_FRAME,function(e:Event):void
            {
                emitter.x = _mx;
                emitter.y = _my;
                emitter.emit();
                
                emitter.update();
                
                particlesLength = emitter.particles.length;
				
                var p:Particle;
                for(iParticles = 0; iParticles < particlesLength; iParticles++)
                {
                    p = emitter.particles[iParticles];
                    var r:Number = p.lifeTime / p.maxLifeTime;
					//trace(r * 0xff << 24, 0xFF00FF00);
                  
                    canvas.setPixel32(p.x, p.y, 0xFF00AA00);// r * 0xff << 24 | (canvas.getPixel32(p.x, p.y) & 0xff000000) );
                }
                canvas.applyFilter( canvas, _rect, _ep, blurFilter );
               
                buffer.lock();
                buffer.applyFilter( buffer, _rect, _ep, blurFilter2 );
                buffer.colorTransform(_rect, ct);
               
                buffer.copyPixels(canvas, _rect, _ep, null, null, true);
                buffer.unlock();
            });
        }
    }
}